﻿Imports System.Data
Partial Class pukl_1
    Inherits System.Web.UI.Page
    Public shopingCount As String
    Protected dt As Data.DataTable
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblMsg.Text = String.Empty
        Title = "以車型產品方式查詢..."        
        If (Not IsPostBack) Then
            fillControl()
            showShopButton()
            Utility.LoadAutoComplete(Me, Me.GetType)

            System.Web.UI.ScriptManager.RegisterClientScriptBlock( _
                Me, Me.GetType(), "autoCar", ucCARac.GetAutoCompleteScript(), True)

            System.Web.UI.ScriptManager.RegisterClientScriptBlock( _
                Me, Me.GetType(), "autProd", ucPRODUCTac.GetAutoCompleteScript(), True)
        End If
        hideDisplay()
    End Sub
    Protected Sub btnExecute_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim btn As Button
        btn = CType(sender, Button)
        execute(btn.ID)
    End Sub
    Protected Sub execute(ByVal p_execID As String)
        If (p_execID.Equals("btnExecute")) Then
            getData(ucCAR.GetSelectItemValue(), ucPRODUCT.GetSelectItemValue())
        Else
            getData(ucCARac.GetName(), ucPRODUCTac.GetName())
        End If

    End Sub
    Protected Sub getData()
        Dim car, prod As String
        car = Session("car")
        prod = Session("prod")
        dt = Utility.GetCarProdData(car, prod)
        GV1.DataSource = dt
        GV1.DataBind()
        showShopButton()
    End Sub

    Protected Sub getData(ByVal p_car As String, ByVal p_prod As String)
        Session("car") = p_car.Trim()
        Session("prod") = p_prod.Trim()
        getData()
    End Sub
    Protected Sub fillControl()
        Dim carDT, productDT As DataTable
        carDT = Utility.GetCarType()
        productDT = Utility.GetProduct()

        ucCAR.SetData(carDT, 0, 0)
        ucPRODUCT.SetData(productDT, 0, 0)
    End Sub

    Protected Sub GV1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GV1.PageIndexChanging
        GV1.PageIndex = e.NewPageIndex
        getData()
    End Sub

    Protected Sub GV1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GV1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim lbl As Label
            lbl = CType(e.Row.Cells(0).FindControl("lblPu_id"), Label)
            lbl.Text = dt.Rows(e.Row.RowIndex)("PUID").ToString()
        End If
    End Sub
    Protected Sub showShopButton()
        Dim script As String
        script = String.Empty
        Utility.LoadJquery(Me, Me.GetType())
        If (GV1.Rows.Count.Equals(0)) Then

            script = " $(function()" + _
                "{" + _
                "    $('.shoping').hide();" + _
                "});"
        Else
            script = " $(function()" + _
                    "{" + _
                    "    $('.shoping').show();" + _
                    "});"
        End If
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "SHOpPING", script, True)
    End Sub

    Protected Sub btnShoping_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShoping.Click
        shoping()
    End Sub
    Protected Sub btnShoping1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShoping1.Click
        shoping()
    End Sub

    Protected Sub shoping()
        '--還要有一個顯示東西的網頁
        Dim chk As CheckBox
        Dim lbl As Label
        Dim shopCount As New ArrayList

        For Each row As GridViewRow In GV1.Rows
            chk = CType(row.Cells(0).FindControl("chkShoping"), CheckBox)
            If Not (chk Is Nothing) Then
                If (chk.Checked) Then
                    lbl = CType(row.Cells(0).FindControl("lblPu_id"), Label)
                    shopCount.Add(lbl.Text)
                End If
            End If
        Next
        Session("shoplist") = shopCount
        shopingCount = shopCount.Count.ToString()
    End Sub

    Protected Sub hideDisplay()
        Dim script As String
        script = "$(document).ready(function(){$('.autoDisplay').hide();})"
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "autoDisplay", script, True)
    End Sub

End Class

